package org.devocean.groupbuying.web.backend;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.devocean.groupbuying.domain.PurchaseItem;
import org.devocean.groupbuying.service.PaypalPaymentService;
import org.devocean.groupbuying.service.PurchaseItemService;
import org.devocean.groupbuying.utils.RenderMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping(value = { "/gb-cpanel/payment/paypal" })
public class PaypalController {

	@Autowired
	private PaypalPaymentService paypalPaymentService;
	
	@Autowired
	private PurchaseItemService purchaseItemService;

	@RequestMapping(value = "/capture")
	public Map<String, ? extends Object> capturePayment(
			@RequestParam(value = "purchaseId") Integer purchaseId) {
		try {
			paypalPaymentService.capturePayment(purchaseId);
			Map<String, Object> data = new ConcurrentHashMap<String, Object>();
			PurchaseItem purchaseItem = purchaseItemService.getPurchaseItemById(purchaseId);
			data.put("status", purchaseItem.getStatus());
			return RenderMapUtils.getMap(data);

		} catch (Exception e) {
			return RenderMapUtils
					.getModelMapError("Error capturing purchaise item.");

		}

	}

}
